package com.bosssoft.trainee.service.imp;

import com.bosssoft.trainee.common.service.RedisService;
import com.bosssoft.trainee.mbg.mapper.UserMapper;
import com.bosssoft.trainee.mbg.po.User;
import com.bosssoft.trainee.service.UserCacheService;
import com.bosssoft.trainee.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Author: wuxin
 * @Project: rbac
 * @Date: 2022/6/23
 * @Description:
 */
@Service
@Slf4j
public class UserCacheServiceImp implements UserCacheService {
    @Autowired
    private RedisService redisService;
    @Autowired
    private UserService userService;
    @Value("${redis.database}")
    private String redisDatabase;
    @Value("${redis.expire.common}")
    private Long redisExpire;
    @Value("${redis.key.user}")
    private String redisUserKey;
    @Value("${redis.key.resourceList}")
    private String redisResourceListKey;

    /**
     * 删除后台用户缓存
     *
     * @param userId
     */
    @Override
    public void delUser(int userId) {
        User user = userService.getById(userId);
        if (user != null) {
            String key = redisDatabase + ":" + redisUserKey + ":" + user.getUsername();
            log.info("key => {}" + key);
            redisService.del(key);
        }
    }
    @Override
    public void delResourceList(Long adminId) {
        String key = redisDatabase + ":" + redisResourceListKey + ":" + adminId;
        redisService.del(key);
    }

    /**
     * 获取缓存后台用户信息
     *
     * @param username
     */
    @Override
    public User getUser(String username) {
        String key = redisDatabase + ":" + redisUserKey + ":" + username;
        return (User) redisService.get(key);
    }

    /**
     * 设置缓存后台用户信息
     *
     * @param user
     */
    @Override
    public void setUser(User user) {
        String key = redisDatabase + ":" + redisUserKey + ":" + user.getUsername();
        redisService.set(key, user, redisExpire);
    }
}
